﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WallpaperControl
{
    /// <summary>
    /// Condition that changes the wallpaper on every startup.
    /// </summary>
    internal class CondStartup : Condition
    {
        /// <summary>
        /// Constructor. Creates a new condition object of type startup.
        /// </summary>
        /// <param name="activeWallpaper">The zero-based index of the 
        /// last active wallpaper.</param>
        public CondStartup(int activeWallpaper)
        {
            this.ActiveWallpaper = activeWallpaper;

            Program.ControlPanel.radioButtonStartup.Checked = true;
        }

        /// <summary>
        /// The ID of this condition.
        /// </summary>
        internal const int CondId = 3;

        /// <summary>
        /// The index of the last active wallpaper.
        /// </summary>
        private int _activeWallpaper;

        internal override int Id
        {
            get 
            {
                return CondStartup.CondId;
            }
        }
        
        /// <summary>
        /// Gets or sets the index of the last active wallpaper.
        /// </summary>
        /// <value>The zero-based index of the previous wallpaper.</value>
        public int ActiveWallpaper
        {
            get
            {
                return this._activeWallpaper;
            }
            set
            {
                if (value <= ImageList.Instance.Images.Count)
                {
                    this._activeWallpaper = value;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }

        /// <summary>
        /// The time until the next change is unknown.
        /// </summary>
        internal override TimeSpan RemainingTime
        {
            get
            {
                return TimeSpan.Zero;
            }
        }

        /// <summary>
        /// Gets or set the value for this condition.
        /// </summary>
        internal override object Value
        {
            get
            {
                return this.ActiveWallpaper;
            }
            set
            {
                this.ActiveWallpaper = Convert.ToInt32(value);
            }
        }

        /// <summary>
        /// Checks if the current condition is true and executes the wallpaper 
        /// change.
        /// </summary>
        internal override void Check()
        {
            if (Program.CurrentSession.IsActive)
            {
                // Get the index of the next wallpaper
                int nextWallpaper = this.ActiveWallpaper + 1;
                // Make sure wallpaper is in the list
                if (nextWallpaper < ImageList.Instance.Images.Count)
                {
                    WallpaperManager.Instance.SetWallpaper(nextWallpaper);
                    this.ActiveWallpaper = nextWallpaper;
                }
                else
                {
                    if (Program.Configuration.LoopWallpapers)
                    {
                        // Begin again with first image
                        this.ActiveWallpaper = -1;
                        this.Check();
                    }
                    else
                    {
                        // Begin with first image next time
                        this.ActiveWallpaper = -1;
                        // Stop checking
                        Program.CurrentSession.Stop();
                    }
                }
            }
        }

        /// <summary>
        /// Nothing to handle.
        /// </summary>
        internal override void ManualChangeCallback()
        {
        }

        internal override void StopSession()
        {
        }

        /// <summary>
        /// No value to apply.
        /// </summary>
        internal override void ApplyValue()
        {
        }
    }
}
